package com.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.Remark;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.NotFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.RemarkNode;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.ScriptTag;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.SimpleNodeIterator;
import org.mk.type.IntegerType;
import org.mk.type.MySQLTypeFactory;
import org.mk.type.Type;

import com.db.ColumnInfoBean;
import com.db.ForeignKey;
import com.excel.read.Config;
import com.excel.read.DBTable;
import com.operation.OperationMap;
import com.operation.OperationMap;
import com.operation.RecordOperation;
import com.operation.model.OperationModel;

/**
 * 爬虫类，根据某一个标题来查找文章，并且去掉多余的标签，只留下文本字段。 页面展现1
 * "DIV.1,DIV.3,DIV.1,DIV.1,DIV.6,ul.1" 页面展现2
 * DIV.1,DIV.2,DIV.3,DIV.4,DIV.3,form.1,ul.1 取得记录值对
 * 
 * @author michael
 * 
 */
public class RecordExtract extends Extract {

	// 循环访问所有节点，输出包含关键字的值节点
	public OperationModel extractOperationMap(String html, long storeFrom)
			throws Exception {
		Parser parser = new Parser();
		parser.setInputHTML(html);
		parser.setEncoding("gb2312");
		// 迭代所有节点, null 表示不使用 NodeFilter
		TagNameFilter tf = new TagNameFilter("TABLE");
		NotFilter ntFilter = new NotFilter(new NodeClassFilter(ScriptTag.class));
		AndFilter rorFilter = new AndFilter(tf, ntFilter);
		NodeList list = parser.parse(tf);

		// 确定是内容节点的时候做
		MyNotFilter myNotFilter = new MyNotFilter();
		// div
		myNotFilter.accept(list.elementAt(0));

		// 取得Map的条数
		TableTag tn = (TableTag) list.elementAt(0);
		OperationMap opMap = new OperationMap(storeFrom);
		List<String> tableNames = new ArrayList<String>();
		Map tableMap = new HashMap();
		List<Record> aList = getRecordList(tn, storeFrom);
		String tableName = aList.get(0).getTableName();
		String operation = aList.get(0).op;
		if (operation != null && operation.equalsIgnoreCase("insert")) {
			opMap.opTableName = tableName;
			opMap.record = aList.get(0);
		}

		if (operation != null && operation.equalsIgnoreCase("update")) {
			opMap.opTableName = tableName;
			opMap.record = aList.get(0);
		}

		if (operation != null && operation.equalsIgnoreCase("get")) {
			opMap.opTableName = tableName;
			opMap.record = aList.get(0);
		}

		tableMap.put(tableName, aList);
		tableNames.add(tableName);

		if (operation != null && operation.equalsIgnoreCase("get")) {

		} else {

			// 从数据库中取得本地外键记录
			DBTable dbTable = (DBTable) Config.allTable.get(opMap.record
					.getTableName());
			// 所有外键
			List<ForeignKey> fkList = dbTable.fkList;
			// 对每一个外键都增加一条外键记录
			for (ForeignKey aForeignKey : fkList) {
				String sql = RecordOperation.getQueryIDValue(aForeignKey);
				List<String> fields = new ArrayList<String>();
				tableName = aForeignKey.getToTable().getName();
				if (tableName.equalsIgnoreCase("ad_language")) {
					int x = 0;
					x++;
				}
				String pkName = aForeignKey.getToTable().getPk().getName();

				fields.add(pkName);
				for (ColumnInfoBean cinfo : aForeignKey.getToTable().realPK) {
					if (!pkName.equalsIgnoreCase(cinfo.getName())) {
						fields.add(cinfo.getName());
					}

				}

				Object pkValue = opMap.record.fieldMap.get(pkName);

				DBTable toTable = aForeignKey.getToTable();
				ColumnInfoBean info = toTable.getColumnInfo(pkName
						.toUpperCase());
				Type type = MySQLTypeFactory.basic(info
						.getNoPresitionTypeName());
				List<Record> aList2 = RecordOperation.getPKValue(aForeignKey
						.getToTable().getName(), fields, sql, type, pkValue);
				tableMap.put(tableName, aList2);
				tableNames.add(tableName);
			}
		}
		opMap.aMap = tableMap;
		opMap.tableNames = tableNames;

		return opMap;

	}

	public boolean hasComment(Node node) {
		if (node == null) {
			return false;
		}
		NodeList list = node.getChildren();

		// 如果子结点为空，返回
		if (list == null || list.size() == 0) {
			return false;
		}
		ArrayList arrayList = new ArrayList();
		int size = list.size();
		// 先标志需要删除的节点集合
		for (int i = 0; i < size; i++) {
			Node aNode = list.elementAt(i);
			if (aNode instanceof Remark) {
				if (aNode.getText().indexOf("正文块 begin") != -1) {
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * 取得第几个节点
	 * 
	 * @param url
	 * @param list
	 * @param keyword
	 */
	private Node getNode(Node node, String tagName, int index) {
		// 迭代开始
		NodeList childList = node.getChildren();
		SimpleNodeIterator iterator = childList.elements();
		int aIndex = 0;
		while (iterator.hasMoreNodes()) {
			Node aNode = iterator.nextNode();
			if (aNode instanceof TextNode || aNode instanceof RemarkNode) {
				continue;
			}
			if (tagName.equalsIgnoreCase(((Tag) aNode).getTagName())) {
				aIndex++;
				if (aIndex == index) {
					return aNode;
				}
			}
		}
		return null;
	}

	/**
	 * 根据层次得到节点 DIV.1 DIV.2
	 * 
	 * @param url
	 * @param filter:
	 *            DIV.1,DIV.2
	 * @param keyword
	 */
	private List<Record> getNodeList(Node node, String filter) {
		List<Record> recordList = new ArrayList<Record>();
		String[] filters = filter.split(",");
		Node curNode = node;
		for (String aFilter : filters) {
			String[] subs = aFilter.split("\\.");
			String tagName = subs[0];
			String index = subs[1];
			Node aNode = getNode(curNode, tagName, Integer.parseInt(index));
			curNode = aNode;

		}

		Node root = curNode;
		// TableTag
		TableTag aTableTag = (TableTag) root;
		int rowCount = aTableTag.getRowCount();
		int colCount = 0;
		TableRow tr = null;
		String[] tdc = null;
		// 第一列是列名
		TableColumn[] tds = null;
		if (rowCount > 0) {
			tr = aTableTag.getRow(0);
			colCount = tr.getColumnCount();
			if (colCount > 0) {
				tds = tr.getColumns();
				tdc = new String[colCount];
				for (int i = 0; i < colCount; i++) {
					tdc[i] = tds[i].toPlainTextString();
				}
			}

		}

		// 以后都是数据
		if (rowCount > 1) {
			Record aRecord = null;
			for (int i = 1; i < rowCount; i++) {
				tr = aTableTag.getRow(i);
				TableColumn[] fvs = tr.getColumns();
				aRecord = new Record();
				aRecord.setTableName(aTableTag.getAttribute("name"));

				String operation = aTableTag.getAttribute("operation");
				aRecord.op = operation;

				for (int j = 0; j < colCount; j++) {
					String fieldName = tdc[j];
					String value = fvs[j].toPlainTextString();
					aRecord.put(fieldName, value);
				}
				recordList.add(aRecord);
			}

		}
		return recordList;
	}

	private List<Record> getRecordList(TableTag aTableTag, long storeFrom) {

		List<Record> recordList = new ArrayList<Record>();
		int rowCount = aTableTag.getRowCount();
		int colCount = 0;
		TableRow tr = null;
		String[] tdc = null;
		// 第一列是列名
		TableColumn[] tds = null;
		if (rowCount > 0) {
			tr = aTableTag.getRow(0);
			colCount = tr.getColumnCount();
			if (colCount > 0) {
				tds = tr.getColumns();
				tdc = new String[colCount];
				for (int i = 0; i < colCount; i++) {
					tdc[i] = tds[i].toPlainTextString();
				}
			}

		}

		// 以后都是数据
		if (rowCount > 1) {
			Record aRecord = null;
			for (int i = 1; i < rowCount; i++) {
				tr = aTableTag.getRow(i);
				TableColumn[] fvs = tr.getColumns();
				aRecord = new Record();
				aRecord.setTableName(aTableTag.getAttribute("name"));
				aRecord.storeFrom = storeFrom;
				String operation = aTableTag.getAttribute("operation");
				aRecord.op = operation;

				for (int j = 0; j < colCount; j++) {
					String fieldName = tdc[j];
					String value = fvs[j].toPlainTextString();
					aRecord.put(fieldName.toUpperCase(), value);
				}
				recordList.add(aRecord);
			}

		}
		return recordList;
	}

	/**
	 * 取得产品的信息
	 * 
	 * @param node
	 */
	private String getNodeText(Node node, String filter) {
		String[] filters = filter.split(",");
		Node curNode = node;
		for (String aFilter : filters) {
			String[] subs = aFilter.split("\\.");
			String tagName = subs[0];
			String index = subs[1];
			Node aNode = getNode(curNode, tagName, Integer.parseInt(index));
			curNode = aNode;

		}

		return curNode.toPlainTextString();
	}

	private String getAttribute(Node node, String filter, String atrName) {
		String[] filters = filter.split(",");
		Node curNode = node;
		for (String aFilter : filters) {
			String[] subs = aFilter.split("\\.");
			String tagName = subs[0];
			String index = subs[1];
			Node aNode = getNode(curNode, tagName, Integer.parseInt(index));
			curNode = aNode;

		}
		return ((TagNode) curNode).getAttribute(atrName);
	}

}
